import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;



class IntCmp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}

public class Test {

    public static void main(String[] args) {
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>(new IntCmp());

        priorityQueue.offer(3);
        priorityQueue.offer(13);

    }
    public int[] smallestK2(int[] array, int k) {
        int[] ret = new int[k];
        if(array == null || k <= 0) {
            return ret;
        }
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>(new IntCmp());



        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }

        for (int i = k; i < array.length; i++) {
            int top = priorityQueue.peek();
            if(array[i] < top) {
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }

    public int[] smallestK1(int[] array, int k) {
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>(array.length);

        for (int i = 0; i < array.length; i++) {
            priorityQueue.offer(array[i]);
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }

    public static int[] maxLestK(int[] array, int k) {
        int[] ret = new int[k];
        if(array == null || k <= 0) {
            return ret;
        }
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }

        for (int i = k; i < array.length; i++) {
            int top = priorityQueue.peek();
            if(array[i] > top) {
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }





    public static void main12(String[] args) {
        int[] array = {10,4,2,6,15,24,9,0,4};
        int[] ret2 = maxLestK(array,3);
        System.out.println(Arrays.toString(ret2));


        //默认情况下是一个小根堆
        /*PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>();
        priorityQueue.offer(10);
        priorityQueue.offer(5);
        priorityQueue.offer(9);
        priorityQueue.offer(7);
        priorityQueue.offer(77);
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        */

        /*PriorityQueue<Student> priorityQueue1 = new PriorityQueue<>();
        priorityQueue1.offer(null);*/




    }
    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initHeap(array);

        testHeap.createHeap();

        System.out.println("===插入元素==");
        //testHeap.offer(80);
        System.out.println("===删除===");
        int val = testHeap.poll();
        System.out.println(val);
        System.out.println("====");
    }
}
